using System.Linq;
using Daemon.Common;
using Daemon.Common.Helpers;
using Daemon.Infrustructure.EF;
using Daemon.Repository.EF.Enums;
using Daemon.Repository.Contract;
using Daemon.Model;
using System;
using System.Linq.Expressions;
using System.Collections.Generic;

namespace Daemon.Repository.EF
{
    public class ${modelName}Repository : Repository<${modelName}>, I${modelName}Repository
    {
        protected override Expression<Func<${modelName}, int>> PrimaryKeyExpression => r => r.Id;

        public ${modelName}Repository(ApiDBContent context) : base(context)
        {
        }

        ${if(relationshipCount>0)}
        protected override IEnumerable<${modelName}> IncludeRelationships(IEnumerable<${modelName}> entities, ApiDBContent db)
        { 
            $for(dic in dicArray)
            var ${dic} = new new Dictionary<int,${mapArray[dic]}>();
            $end
            $for(relationship in relationships)
            if(RouteParameterHelper.CheckRouteParameter(RelationshipNames,${modelName}Relationship.${relationship}))
            {
                ${dicRelationships[relationship]} = GetRelationshipDict<${mapArray[relationship]}, int, ${mapArray[relationship]}, ${modelName}Relationship>(entities.ToList(), r => true, r => r.${relationshipForeignColumns[relationship]}, r => true, r => r.${relationshipMainColumns[relationship]}, r => r, ${modelName}Relationship.${relationship}, db);
            }
            $end

            foreach (var entity in entities)
            {
                $for(dic in dicArray)
                if(${dic}.TryGetValue(entity.${dicMainColumns[dic]},out ${dicForeignEntities[dic]} ${dicTempNames[dic]}))
                { 
                    entity.${foreignPropertyNames[dic]} = ${dicTempNames[dic]};
                }
                $end
            }
        }
        $end
    }
}
